package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Fraisforfait;
import net.models.Praticiens;
import net.models.TypesPraticiens;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.hibernate.Transaction;

public class FraisForfaitEditer {

        protected Shell shellFraisForfaitEditer;
        private Text    txtAjoutDeFrais;
        private Text    textf1;
        private Text    textf2;
        private Text    textf3;
        private Text    textf4;
        private Text    text4;
        private Text    text3;
        private Text    text2;
        private Text    text1;
    	private List<Fraisforfait>		fraisforfaits		= new ArrayList<Fraisforfait>();

        /**
         * Launch the application.
         * 
         * @param args
         */
        public static void main(String[] args) {
                try {
                        FraisForfaitEditer window = new FraisForfaitEditer();
                        window.open();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        /**
         * Open the window.
         */
        public void open() {
        		fraisforfaits = Application.getFraisforfaits();
                Display display = Display.getDefault();
                createContents();
                shellFraisForfaitEditer.open();
                shellFraisForfaitEditer.layout();
                while (!shellFraisForfaitEditer.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();
                        }
                }
        }

        /**
         * Create contents of the window.
         */
        protected void createContents() {
                shellFraisForfaitEditer = new Shell();
                shellFraisForfaitEditer.setSize(391, 379);
                shellFraisForfaitEditer.setText("SWT Application");

                Group grpFraisForfait = new Group(shellFraisForfaitEditer, SWT.NONE);
                grpFraisForfait.setText("Frais Forfait");
                grpFraisForfait.setBounds(10, 57, 349, 221);

                CLabel lblNewLabel_1 = new CLabel(grpFraisForfait, SWT.NONE);
                lblNewLabel_1.setBounds(10, 27, 76, 26);
                lblNewLabel_1.setText("Forfaits");

                CLabel lblTotal = new CLabel(grpFraisForfait, SWT.NONE);
                lblTotal.setBounds(182, 27, 76, 26);
                lblTotal.setText("Total");

                textf1 = new Text(grpFraisForfait, SWT.BORDER);
                textf1.setBounds(10, 60, 141, 31);

                textf2 = new Text(grpFraisForfait, SWT.BORDER);
                textf2.setBounds(10, 100, 141, 31);

                textf3 = new Text(grpFraisForfait, SWT.BORDER);
                textf3.setBounds(10, 140, 141, 31);

                textf4 = new Text(grpFraisForfait, SWT.BORDER);
                textf4.setBounds(10, 180, 141, 31);

                text4 = new Text(grpFraisForfait, SWT.BORDER);
                text4.setBounds(182, 180, 141, 31);

                text3 = new Text(grpFraisForfait, SWT.BORDER);
                text3.setBounds(182, 140, 141, 31);

                text2 = new Text(grpFraisForfait, SWT.BORDER);
                text2.setBounds(182, 100, 141, 31);

                text1 = new Text(grpFraisForfait, SWT.BORDER);
                text1.setBounds(182, 60, 141, 31);

                Composite composite = new Composite(shellFraisForfaitEditer, SWT.NONE);
                composite.setBounds(154, 284, 209, 52);

                Button button = new Button(composite, SWT.NONE);
                button.addMouseListener(new MouseAdapter() {
                	@Override
                	public void mouseUp(MouseEvent e) {
                		shellFraisForfaitEditer.close();
                	}
                });
                button.setText("Annuler");
                button.setBounds(106, 10, 90, 30);

                Button button_1 = new Button(composite, SWT.NONE);
                button_1.addMouseListener(new MouseAdapter() {
                	@Override
                	public void mouseDown(MouseEvent e) {
                		org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
        				Transaction trans = session.beginTransaction();
        				Fraisforfait fraisForfait1 = fraisforfaits.get(0);
        				Fraisforfait fraisForfait2 = fraisforfaits.get(1);
        				Fraisforfait fraisForfait3 = fraisforfaits.get(2);
        				Fraisforfait fraisForfait4 = fraisforfaits.get(3);
            			Fraisforfait fraisforfait1 = new Fraisforfait(fraisForfait1.getId(), textf1.getText(), Integer.valueOf(text1.getText()));
            			Fraisforfait fraisforfait2 = new Fraisforfait(fraisForfait2.getId(), textf2.getText(), Integer.valueOf(text2.getText()));
            			Fraisforfait fraisforfait3 = new Fraisforfait(fraisForfait3.getId(), textf3.getText(), Integer.valueOf(text3.getText()));
            			Fraisforfait fraisforfait4 = new Fraisforfait(fraisForfait4.getId(), textf4.getText(), Integer.valueOf(text4.getText()));
            			session.update(fraisforfait1);
            			session.update(fraisforfait2);
            			session.update(fraisforfait3);
            			session.update(fraisforfait4);
        				trans.commit();
        				session.close();  
        				shellFraisForfaitEditer.close();
                	}
                });
                button_1.setText("Valider");
                button_1.setBounds(10, 10, 90, 30);

                Composite composite_1 = new Composite(shellFraisForfaitEditer, SWT.NONE);
                composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
                composite_1.setBounds(0, 0, 375, 42);

                txtAjoutDeFrais = new Text(composite_1, SWT.READ_ONLY);
                txtAjoutDeFrais.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
                txtAjoutDeFrais.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
                txtAjoutDeFrais.setForeground(SWTResourceManager.getColor(255, 255, 255));
                txtAjoutDeFrais.setText("Modification de frais forfaitaire");
                txtAjoutDeFrais.setBounds(10, 10, 278, 20);
                
                textf1.setText(fraisforfaits.get(0).getLibelle());
                textf2.setText(fraisforfaits.get(1).getLibelle());
                textf3.setText(fraisforfaits.get(2).getLibelle());
                textf4.setText(fraisforfaits.get(3).getLibelle());
                
                text1.setText(String.valueOf(fraisforfaits.get(0).getMontant()));
                text2.setText(String.valueOf(fraisforfaits.get(1).getMontant()));
                text3.setText(String.valueOf(fraisforfaits.get(2).getMontant()));
                text4.setText(String.valueOf(fraisforfaits.get(3).getMontant()));

        }
}